/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package post;

import java.io.*;
import java.util.Scanner;
import java.util.Date;

public class TransactionReader {

    Store store;
    BufferedReader transactions;

    public TransactionReader(Store newStore, String transactionFile) {
        this.store = newStore;
        try {
            transactions = new BufferedReader(new FileReader(transactionFile));
        } catch (FileNotFoundException e) {
            System.err.println("FileNotFoundException: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("IOException: " + e.getMessage());
        }
    }

    public boolean hasMoreTransactions() {
        try {
            return transactions.ready();
        } catch (IOException e) {
            System.err.println("IOException: " + e.getMessage());
            return false;
        }
    }

    public Transaction getNextTransaction() {
        String transaction_info[] = new String[100];
        IPayment payment;
        int counter = -1;
        int array_size;
        Date datetime = new Date();

        try {
          //String Line=transactions.readLine();

//          while(transactions.ready() && (transaction_info[counter]=transactions.readLine()) != ""){
//              
//              //Line=transactions.readLine();
//              counter++;
//          }
            do {
                transaction_info[++counter] = transactions.readLine();
            } while (transactions.ready() && (!transaction_info[counter].isEmpty()));

            if (transaction_info[counter].isEmpty()) {
                counter--;
            }
            array_size = transaction_info.length;
          //System.out.println(array_size);

            //Create TransactionHeader for current Transaction
            TransactionHeader cust_info = new TransactionHeader(datetime, transaction_info[0]);

            //Create Transaction using created TransactionHeader
            Transaction transaction = new Transaction(cust_info);

            //Read and Set payment for current Transaction
            String payment_option = transaction_info[counter];

            if ("CASH".equals(payment_option.substring(0, 4))) {
                //System.out.println(payment_option.substring(0, 4));
                float payment_amount = Float.parseFloat(payment_option.substring(6));
                payment = new CashPayment( payment_amount);
            } else if ("CHECK".equals(payment_option.substring(0, 5))) {
                float payment_amount = Float.parseFloat(payment_option.substring(7));
                payment = new CheckPayment( payment_amount);

            } else {
                float payment_amount = Float.parseFloat(payment_option.substring(7));
                payment = new CreditPayment( payment_amount);
            }

            transaction.setPayment(payment);

          ///create trasaction header name the date
            //for each item we have to create a transaction item object
            //then call add transaction method in the transaction class
            int item_quant;

            for (int i = 1; i <= counter - 1; i++) {
                //upc item
                String upc_item = transaction_info[i].substring(0, 4);

              //System.out.println(i);
                //System.out.println(upc_item);
                if (this.store.validateUPC(upc_item)) {
                    //upc quantity
                    if (transaction_info[i].length() <= 4) {
                        item_quant = 1;
                        //System.out.println("item quant: " + item_quant);
                    } else {
                        item_quant = Integer.parseInt(transaction_info[i].substring(9));
                        //System.out.println("item quant: " + item_quant);
                    }

              //Create and Set TransactionItem and quantity for Current Transaction
                    TransactionItem quantity =store.getItem(upc_item);
                    quantity.setQuantity(item_quant);
                    transaction.addTransactionItem(quantity);
                }
            }

            return transaction;
        } catch (IOException e) {
            return null;
        }

    }
//    public static void main(String[] args){
//        Store newstore= new Store("newStore");
//        TransactionReader line= new TransactionReader(newstore, "transaction.txt");
//        line.getNextTransaction();
//    }

}
